<?php

namespace App\Http\Controllers\Api;


use App\Chain;
use App\Cinema;
use App\City;
use App\OnlineFilmticket;
use App\SoftwareVersion;
use App\User;
use Illuminate\Http\Request;
use JWTAuth;

class ChainsController extends BaseController
{
    public $user;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->user = $this->is_login($request);
    }

    //查看影院列表
    public function index(Request $request){

        if (!$this->user['data']->can('cinema_list')) {
            return error('NoPermission');
        }
        $data =Cinema::where('cinemas.status','!=',3)
            ->join('users','users.cinema_id','=','cinemas.id')
            ->where('users.is_manager','=',1)
            ->select('cinemas.*','users.account as account','users.id as user_id')
            ->paginate(15);
        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * @return array
     * 重置影院管理员密码
     */
    public function change_cinema_reset_password(Request $request){
        if (!$this->user['data']->can('cinema_list')) {
            return error('NoPermission');
        }

        if (!$id = $request->id){
            return error('影院管理员用户id缺失');
        }

        $user_info = User::where(['is_manager'=>1,'type'=>2])->find($id);

        if (!$user_info){
            return error('影院管理员不存在');
        }

        $user_info->password = bcrypt($user_info->account.'1234');
        if ($user_info->save()){
            return success('','重置密码成功');
        }else{
            return error('重置密码失败');
        }

    }

    //查看院线信息
    public function info(Request $request){
        if (!$this->user['data']->can('cinema_manager')) {
            return error('NoPermission');
        }
        $data = Chain::first();
        $data->province = get_city_name($data->province);
        $data->city = get_city_name($data->city);
        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * @return array
     * 查询影院信息
     */
    public function get_cinema_info(Request $request){
        if (!$this->user['data']->can('cinema_manager')) {
            return error('NoPermission');
        }

        if (!$id = trim($request->id)){
            return error('影院id缺失');
        }
        $cinema_info = Cinema::find($id);
        if (!$cinema_info){
            return error('影院信息不存在');
        }else{
            return success($cinema_info,'查询成功');
        }
    }

    /**
     * 添加影院
     */
    public function add_cinema(Request $request){
        if (!$this->user['data']->can('cinema_manager')) {
            return error('NoPermission');
        }

        if (!$name = trim($request->name)){
            return error('影院名称必填');
        }
        if (!$account = trim($request->account)){
            return error('影院账户必填');
        }
        $cinema_info = Cinema::where(['name'=>$name])->first();
        if ($cinema_info){
            return error('影院已存在');
        }
        $check_account = User::where(['account'=>$account])->count();
        if ($check_account>0){
            return error('该账号已存在');
        }

        $password = $request->password;

        //添加影院信息
        $cinema = new Cinema();
        $cinema->name = $name;
        $cinema->corporation = $request->corporation;
        $cinema->contact = $request->contact;
        $cinema->telephone = $request->telephone;
        $cinema->chain_code = $this->user['data']['chain_code'];
        $cinema->province = $request->province;
        $cinema->city = $request->city;
        $cinema->district = $request->district;
        $cinema->address = $request->address;
        $cinema->status = 0;
        if($cinema->save()){
            $cinema_user = new User();
            $cinema_user->name = $name;
            $cinema_user->account = $account;
            if ($password){
                $cinema_user->password = bcrypt($password);
            }else{
                $cinema_user->password = bcrypt($cinema_user->account.'1234');
            }
            $cinema_user->chain_code = $this->user['data']['chain_code'];
            $cinema_user->cinema_id = $cinema->id;
            $cinema_user->type = 2;
            $cinema_user->is_manager = 1;
            $cinema_user->save();
            $admin = \App\Role::where(['name'=>'影院管理员','type'=>2,'status'=>0])->first();
            $cinema_user->attachRole($admin);
            logs($this->user,'添加影院'.$name.'成功');
            return success($cinema_user->id,'添加成功，影院账户：'.$account);
        }else{
            logs($this->user,'添加影院'.$name.'失败');
            return error('添加失败');
        }
    }

    /**
     * 编辑影院
     */
    public function edit_cinema(Request $request){

        if (!$this->user['data']->can('cinema_manager')) {
            return error('NoPermission');
        }

        if (!$id = trim($request->id)){
            return error('影院id缺失');
        }
        $cinema = Cinema::where('status','!=',3)->find($id);
        if (!$cinema){
            return error('影院不存在');
        }
        if (!$name = trim($request->name)){
            return error('影院名称必填');
        }
        $cinema->name = $name;
        $cinema->corporation = $request->corporation;
        $cinema->contact = $request->contact;
        $cinema->telephone = $request->telephone;
        $cinema->chain_code = $this->user['data']['chain_code'];
        $cinema->province = $request->province;
        $cinema->city = $request->city;
        $cinema->district = $request->district;
        $cinema->address = $request->address;
        if($cinema->save()){
            logs($this->user,'编辑影院'.$name.'成功');
            return success($cinema->id,'编辑成功');
        }else{
            logs($this->user,'编辑影院'.$name.'失败');
            return error('编辑失败');
        }
    }

    /**
     * 删除影院
     */
    public function delete_cinema(Request $request){

        if (!$this->user['data']->can('cinema_manager')) {
            return error('NoPermission');
        }

        if (!$id=$request->input('id')){
            return error('id参数缺失');
        }
        $cinema = Cinema::where('status','!=','3')->find($id);
        if (!$cinema){
            return error('影院不存在或已删除');
        }
        $cinema->status = 3;
        if($cinema->save()){
            logs($this->user,'删除影院'.$cinema->name.'成功');
            return success($cinema->id,'删除成功');
        }else{
            logs($this->user,'删除影院'.$cinema->name.'失败');
            return error('删除失败');
        }
    }

    /**
     * @param Request $request
     * @return array
     * 恢复成功
     */
    public function restore_cinema(Request $request){

        if (!$this->user['data']->can('cinema_manager')) {
            return error('NoPermission');
        }

        if (!$id=$request->id){
            return error('id参数缺失');
        }
        $cinema = Cinema::where('status','=','3')->find($id);
        if (!$cinema){
            return error('影院不存在或已恢复');
        }
        $cinema->status = 0;
        if($cinema->save()){
            logs($this->user,'恢复影院'.$cinema->name.'成功');
            return success($cinema->id,'恢复成功');
        }else{
            logs($this->user,'恢复影院'.$cinema->name.'失败');
            return error('恢复失败');
        }
    }

    /**
     * @param Request $request
     * 获取网售公钥
     */
    public function get_onilne_film_ticketer_publishkey(Request $request){

        if (!$this->user['data']->can('online_ticketer_list')) {
            return error('NoPermission');
        }

        if (!$cinema_code=trim($request->cinema_code)){
            return error('网售商编码缺失');
        }

        $str = http_build_query(['cinema_code'=>$cinema_code]);
        $res = curl($_ENV['SERVER_D'].'/PublicKeyCertQuery',$str,false);

        $res = json_decode($res,true);

        if ($res&&$res['return_value']==0){
            logs($this->user,'获取网售商编码'.$cinema_code.'公钥：'.$res['public_key'].'成功');
            return success($res['public_key'],'获取成功');
        }else{
            logs($this->user,'获取网售商编码'.$cinema_code.'公钥失败');
            return error('获取公钥失败');
        }

    }

    /**
     * @param Request $request
     * 添加网售渠道商
     */
    public function add_online_film_ticketer(Request $request){

        if (!$this->user['data']->can('online_ticketer_list')) {
            return error('NoPermission');
        }

        if (!$name = trim($request->name)){
            return error('网售商名称必填');
        }
        if (!$account = trim($request->account)){
            return error('网售商账号必填');
        }
        if (!$password = trim($request->password)){
            return error('网售商密码必填');
        }
        if (!$cinema_code = trim($request->cinema_code)){
            return error('网售商编码必填');
        }
        if (!$public_key = trim($request->publish_key)){
            return error('公钥缺失');
        }
        $phone = $request->phone;
        $online_film_ticketer = User::where(['cinema_code'=>$cinema_code,'type'=>3])->first();
        if ($online_film_ticketer){
            return error('该网售商已存在');
        }
        $check_account = User::where(['account'=>$account])->count();
        if ($check_account>0){
            return error('该账号已存在');
        }
        $online_user = new User();
        $online_user->name = $name;
        $online_user->account = $account;
        $online_user->password = bcrypt($password);
        $online_user->cinema_code = $cinema_code;
        $online_user->chain_code = $this->user['data']['chain_code'];
        $online_user->public_key = $public_key;
        $online_user->md5_password = md5($password);
        $online_user->phone = $phone;
        $online_user->type = 3;
        if ($online_user->save()){
            logs($this->user,'添加网售商'.$name.'成功,账户：'.$account);
            return success($online_user,'添加成功');
        }else{
            logs($this->user,'添加网售商'.$name.'失败');
            return error('添加失败');
        }
    }

    /**
     * @param Request $request
     * 编辑网售渠道商
     */
    public function edit_online_film_ticketer(Request $request){

        if (!$this->user['data']->can('online_ticketer_list')) {
            return error('NoPermission');
        }

        if (!$id = $request->id){
            return error('网售渠道商id缺失');
        }
        $info = User::where(['type'=>3])->find($id);
        if (!$info){
            return error('网售渠道是不存在');
        }
        if (!$info->name = $request->name){
            return error('网售渠道商姓名缺失');
        }
        $count = User::where(['type'=>3])->where('id','!=',$id)->count();
        if ($count>0){
            return error('该网售商名称已存在');
        }
        $info->phone = $request->phone;
        if ($info->save()){
            logs($this->user,'编辑网售商'.$info->name.'成功');
            return success($info,'编辑成功');
        }else{
            logs($this->user,'编辑网售商'.$info->name.'失败');
            return error('编辑失败');
        }
    }

    /**
     * @param Request $request
     * 获取网售商列表
     */
    public function get_online_film_ticketer_list(Request $request){

        if (!$this->user['data']->can('online_ticketer_list')) {
            return error('NoPermission');
        }

        $list = \DB::table('users');
        $list->where(['type'=>3]);
        $data = $list->paginate(15);
        return success($data,'查询成功');
    }

    /**
     * 暂停网售商
     */
    public function stop_online_film_ticketer(Request $request){
        
                if (!$this->user['data']->can('online_ticketer_list')) {
                    return error('NoPermission');
                }
        
                if (!$id = $request->id){
                    return error('网售商id缺失');
                }
                $online_film_ticketer = User::where('status','!=',2)->where(['type'=>3])->find($id);
                if (!$online_film_ticketer){
                    return error('网售商不存在或已删除');
                }
        
                // $count = OnlineFilmticket::where(['online_user_id'=>$online_film_ticketer,'status'=>1])->count();
                // if ($count>0){
                //     return error('存在合作中影院，不可删除');
                // }
        
                $online_film_ticketer->status=3;
                if ($online_film_ticketer->save()){
                    logs($this->user,'暂停网售商'.$online_film_ticketer->name.'成功');
                    return success($online_film_ticketer,'暂停成功');
                }else{
                    logs($this->user,'暂停网售商'.$online_film_ticketer->name.'失败');
                    return error('暂停失败');
                }
            }

    /**
     * @param Request $request
     * @return array
     * 删除网售商
     */
    public function delete_online_film_ticketer(Request $request){

        if (!$this->user['data']->can('online_ticketer_list')) {
            return error('NoPermission');
        }

        if (!$id = $request->id){
            return error('网售商id缺失');
        }
        $online_film_ticketer = User::where('status','!=',2)->where(['type'=>3])->find($id);
        if (!$online_film_ticketer){
            return error('网售商不存在或已删除');
        }

        $count = OnlineFilmticket::where(['online_user_id'=>$online_film_ticketer,'status'=>1])->count();
        if ($count>0){
            return error('存在合作中影院，不可删除');
        }

        $online_film_ticketer->status=2;
        if ($online_film_ticketer->save()){
            logs($this->user,'删除网售商'.$online_film_ticketer->name.'成功');
            return success($online_film_ticketer,'删除成功');
        }else{
            logs($this->user,'删除网售商'.$online_film_ticketer->name.'失败');
            return error('删除失败');
        }
    }

    /**
     * @param Request $request
     * @return array
     * 恢复网售商
     */
    public function restore_online_film_ticketer(Request $request){

        if (!$this->user['data']->can('online_ticketer_list')) {
            return error('NoPermission');
        }

        if (!$id = $request->id){
            return error('网售商id缺失');
        }
        $online_film_ticketer = User::find($id);
        if (!$online_film_ticketer){
            return error('网售商不存');
        }
        $online_film_ticketer->status=1;
        if ($online_film_ticketer->save()){
            logs($this->user,'恢复网售商'.$online_film_ticketer->name.'成功');
            return success($online_film_ticketer,'恢复成功');
        }else{
            logs($this->user,'恢复网售商'.$online_film_ticketer->name.'失败');
            return error('恢复失败');
        }
    }

    /**
     * @param Request $request
     * 专资通知
     */
    public function get_zhuanzi_notice(Request $request){

        $title = $request->title;
        $start_time = $request->start_time;
        $end_time = $request->end_time;
        $code = $request->code;

        $zhuanzi_notice = \DB::table('zhuanzi_informs');
        $title&&$zhuanzi_notice->where('title','like','%'.$title.'%');
        $code&&$zhuanzi_notice->where('code','=',$code);
        $start_time&&$end_time&&$zhuanzi_notice->whereBetween('create_time',[$start_time,$end_time]);
        $data = $zhuanzi_notice->paginate(15);
        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * 获取软件版本信息
     */
    public function get_software_version(Request $request){
        if (!$this->user['data']->can('software_version')) {
            return error('NoPermission');
        }
        $info = SoftwareVersion::where('status','=',1)->orderBy('id','desc')->first();
        return success($info,'查询成功');
    }

    /**
     * @param Request $request
     * 更新软件版本信息
     */
    public function update_software_version(Request $request){
        if (!$this->user['data']->can('software_version')) {
            return error('NoPermission');
        }

        if (!$software_MD5 = trim($request->md5)){
            return error('参数缺失');
        }

        $software_info = SoftwareVersion::where(['MD5'=>$software_MD5,'status'=>1])->first();

        if (!$software_info){
            return error('软件MD5不存在');
        }

        $str = http_build_query(['software_MD5'=>$software_MD5]);
        $res = curl($_ENV['SERVER_D'].'/SoftwareUpdateQuery',$str,false);

        $res = json_decode($res,true);

        if (!$res){
            return error('请求失败');
        }

        switch ($res['return_value']){
            case 0:
                $data = SoftwareVersion::orderBy('id','desc')->first();
                return success($data,'获取更新内容成功');
            case 1:
                return error('没有新的更新');
            case 2:
                return error('不允许升级');
            case 3:
                return error('未知错误');
        }
    }

}
